package  com.tianya.common.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.tianya.common.model.Procedure;
import com.tianya.common.util.Utility;
import org.apache.struts2.dispatcher.Dispatcher;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.config.entities.PackageConfig;

public class ActionDao extends HibernateDaoSupport {
	@SuppressWarnings("deprecation")
	public Procedure autoAction(int opid){
		try{ 
			Procedure p = new Procedure();
            String sql="{ CALL SP_AUTOACTION(?,?,?,?,?,?,?,?) }";
            List<String[]> listAction = getAllActionName();
            Connection conn = getSession().connection();
            CallableStatement cs = null;//
            for(String[] strAction : listAction){
            	cs = conn.prepareCall(sql);
            	cs.setString(1, strAction[0]);//请求action
            	cs.setString(2, strAction[1]);//包名
            	cs.setString(3, strAction[2]);//类名
            	cs.setString(4, strAction[3]);//方法名
            	cs.setInt(5, opid);//用户ID
            	cs.registerOutParameter(6,java.sql.Types.INTEGER);//x_status状态
            	cs.registerOutParameter(7,java.sql.Types.VARCHAR);//x_errcode 错误CODE
            	cs.registerOutParameter(8,java.sql.Types.VARCHAR);//x_errmsg 错误消息	
            	cs.execute();
            	p = new Procedure();
            	p.setStatus(cs.getInt(6));//返回值x_status状态
            	p.setCode(cs.getString(7)) ;//返回值x_errcode 错误CODE
            	p.setMessage(cs.getString(8)) ;//返回值x_errmsg 错误消息
            	if(p.getStatus() == 2){
            		break;
            	}
            }
            if(cs != null){
            	cs.close();
            }
           	return p;
        }
        catch(Exception e){ 
            e.printStackTrace(); 
            return null; 
        } 
	}
	@SuppressWarnings("unchecked")
	private List<String[]>  getAllActionName(){
		int maxLength = 2000;
		List<String[]> list = new ArrayList<String[]>();
		Dispatcher dispatcher = Dispatcher.getInstance();   
		ConfigurationManager configurationManager = dispatcher.getConfigurationManager();   
		Configuration config = configurationManager.getConfiguration();    
		Map map =config.getPackageConfigs();
		StringBuffer sb = new StringBuffer();
		StringBuffer packge = new StringBuffer();
		StringBuffer classname = new StringBuffer();
		StringBuffer methodname = new StringBuffer();
		boolean isover = false;
		for (Iterator i = map.values().iterator(); i.hasNext();) {   
			PackageConfig pc = (PackageConfig) i.next();
			String ns = pc.getNamespace();
			if(!Utility.isEmpty(ns)){
				if(ns.startsWith("/")){
					ns = ns.substring(1);
				}
				ns = ns +"/";
			}
			Map<String,ActionConfig> map2 = pc.getActionConfigs();
			
			for(String str:map2.keySet()){
				if(!Utility.isEmpty(str)){
					//如果超长
					isover = false;
					ActionConfig ac = map2.get(str);
					if(sb.toString().length() + str.length() > maxLength){
						isover = true;
					}
					if(packge.toString().length() + Utility.convertToString(ac.getPackageName()).length() > maxLength){
						isover = true;
					}
					if(classname.toString().length() + Utility.convertToString(ac.getClassName()).length() > maxLength){
						isover = true;
					}
					if(methodname.toString().length() + Utility.convertToString(ac.getMethodName()).length() > maxLength){
						isover = true;
					}
					if(isover){
						isover = false;
						list.add(new String[]{sb.toString(),packge.toString(),classname.toString(),methodname.toString()});
						sb = new StringBuffer();
						packge = new StringBuffer();
						classname = new StringBuffer();
						methodname = new StringBuffer();
					}
					sb.append(ns).append(str).append(",");
					
					packge.append(Utility.convertToString(ac.getPackageName())).append(",");
					classname.append(Utility.convertToString(ac.getClassName())).append(",");
					methodname.append(Utility.convertToString(ac.getMethodName())).append(",");
				}
			}
		}
		if(sb.toString().length() > 0){
			list.add(new String[]{sb.toString(),packge.toString(),classname.toString(),methodname.toString()});
		}
		return list;
	}
}
